/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package marsaMaroc.controllers;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import marsaMaroc.dao.AdminDAO;
import marsaMaroc.dao.ArretDAO;
import marsaMaroc.dao.EscaleDAO;
import marsaMaroc.dao.ProduitDAO;
import marsaMaroc.model.Arret;
import marsaMaroc.model.Compte;
import marsaMaroc.model.Escale;
import marsaMaroc.model.Produit;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author Administrateur
 */
@Controller
public class EscaleController {

    ProduitDAO produitDAO = new ProduitDAO();
    ArretDAO arretDAO = new ArretDAO();
    EscaleDAO escaleDAO = new EscaleDAO();

    @RequestMapping(value = "/Produit.htm", method = RequestMethod.GET)
    protected ModelAndView produit() throws Exception {

        return new ModelAndView("client_side/ClientProduit");
    }

    @RequestMapping(value = "/addProduit.htm", method = RequestMethod.POST)
    protected ModelAndView addProduit(
            @RequestParam("code") String code,
            @RequestParam("nom") String nom,
            @RequestParam("categorie") String categorie) throws Exception {

        Produit produit = new Produit();
        produit.setCode(code);
        produit.setNom(nom);
        produit.setCategorie(categorie);
        produitDAO.add(produit);
        System.out.println("bien ajouté");


        return new ModelAndView("client_side/ClientProduit");
    }

    @RequestMapping(value = "/Escale.htm", method = RequestMethod.GET)
    protected ModelAndView escale(HttpServletRequest request) throws Exception {

        List<Produit> produits = escaleDAO.chargerListBox();
        Compte c = (Compte) request.getSession().getAttribute("compte");

        return new ModelAndView("client_side/Demande_Escale", "produits", produits).addObject("c", c.getNomUser());
    }

    @RequestMapping(value = "/addEscale.htm", method = RequestMethod.POST)
    protected ModelAndView addEscale(
            @RequestParam("numNavire") int numNavire,
            @RequestParam("produit") String produit,
            @RequestParam("date_accostage_prevue") String date_accostage_prevue,
            @RequestParam("typeOperation") String typeOperation) throws Exception {

        Escale escale = new Escale();
        escale.setNum_navir(numNavire);
        Produit p = produitDAO.getProduitByCode(produit);
        System.out.println("nommmmmmmm:" + p.getNom());
        escale.setProduit(p);
        escale.setTypeOperation(typeOperation);

        SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
        Date date = formatter.parse(date_accostage_prevue);
        escale.setDate_accostage_prevue(date);        
        escale.setEtat(false);
        
        escaleDAO.add(escale);
        System.out.println(numNavire + "---" + produit + "---" + date_accostage_prevue + "---" + typeOperation);
        return new ModelAndView("client_side/Demande_Escale");
    }

    @RequestMapping(value = "/Arret.htm", method = RequestMethod.GET)
    protected ModelAndView arret() throws Exception {

        return new ModelAndView("admin_side/AjoutArret");
    }

    @RequestMapping(value = "/addArret.htm", method = RequestMethod.POST)
    protected ModelAndView addArret(
            @RequestParam("code") String code,
            @RequestParam("libelle") String libelle) throws Exception {

        Arret arret = new Arret();
        arret.setCode(code);
        arret.setLibelle(libelle);
        arretDAO.add(arret);
        System.out.println("Bienn ajouté");
        return new ModelAndView("admin_side/AjoutArret");
    }
    
    @RequestMapping(value = "/ListeEscale.htm", method = RequestMethod.GET)
    protected ModelAndView listEscale() throws Exception {

        List<Escale> escales = escaleDAO.getAll();
        
        return new ModelAndView("Escale/list_escales","escales",escales);
    }
    @RequestMapping(value = "/deleteEscale.htm", method = RequestMethod.GET)
    protected ModelAndView deleteEscale(
            @RequestParam("id") String id
            ) throws Exception {

        
        int u = Integer.parseInt(id);  
        escaleDAO.delete(u);
        
        return new ModelAndView("Escale/ListeEscale.htm");
    }
    @RequestMapping(value = "/updateEscale.htm", method = RequestMethod.GET)
    protected ModelAndView updateEscale(
            @RequestParam("id") String id
            ) throws Exception {

        int u = Integer.parseInt(id);
        Escale escale = escaleDAO.get(u);
        Date d = escale.getDate_accostage_prevue();
        
        return new ModelAndView("Escale/Update_Escale","escale",escale);
    } 
}
